<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      nickname: {label:'管理员昵称',name:'nickname',type:'text_one',tips:''},
      head_img: {label:'头像',name:'head_img',type:'file_img',tips:'管理员头像'},
      path_alias: {label:'操作内容',name:'path_alias',type:'text_one',config:[],tips:''},
      http_status_code: {label:'状态码',name:'http_status_code',type:'text_one',tips:''},
      ip: {label:'IP地址',name:'ip',type:'text_one',tips:'操作时的客户端IP地址'},
      request: {label:'请求数据',name:'request',type:'json_param',tips:''},
      response: {label:'响应数据',name:'response',type:'json_param',tips:''},
    }
    if (id){
      forms_config.create_time = {label:'操作时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info') ){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/admin_log/edit?id=`+this.id : '/admin/admin_log/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('admin_log/index', '$refs.list_url.list');
          iframe.hide('admin_log/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/admin_log/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
